using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;


using CUtil;
using CStock;
using CIndicators;


namespace CTrading
{
	/// <summary>
	/// Summary description for ConditionTradeSystem.
	/// </summary>
	public class ConditionIndicatorBuilder : System.Windows.Forms.UserControl
	{

		private int IndexCondition;

		private CUtil.IndicatorElement indicatorElement;
		private System.Windows.Forms.TextBox textBoxIndicatorFunctionInterval;
		private System.Windows.Forms.ComboBox comboBoxIndicator;
		private System.Windows.Forms.TextBox textBoxBoxIndicatorInterval;
		private System.Windows.Forms.ComboBox comboBoxIndicatorFunction;
		private System.Windows.Forms.ComboBox comboBoxAddOrSubstract;
		private System.Windows.Forms.TextBox textBoxFactor;
		private System.Windows.Forms.Label labelElement;
		private System.Windows.Forms.ComboBox comboBoxFactor;
        private CheckBox checkBoxIsShare;
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

	
		#region events

		public event ConditionChangedEventHandler ConditionChanged;

		#endregion

		public ConditionIndicatorBuilder(int MyIndexCondition,IndicatorElement MyindicatorElement)
		{
			IndexCondition = MyIndexCondition;
			
			indicatorElement = MyindicatorElement;
			
			InitializeComponent();

			InitializeCustom();

			this.labelElement.Text = MyIndexCondition.ToString();
			
			// TODO: Add any initialization after the InitForm call

		}
		private void InitializeCustom()
		{

			if(indicatorElement == null)
			{
				indicatorElement = new IndicatorElement();
				//InitializeDefaultIndicatorElement();
				
			}

			PopulateControls();
			SetControlSelections();
			//RaiseChangedEvent
		}
        private void FillCombo_Indicators(bool isShare)
        {

            this.comboBoxIndicator.Items.Clear();
            if (isShare)
            {
                IList<string> allShares = TradeEconomy.GetAllShares();
                for (int i = 0; i < allShares.Count; i++)
                {
                    this.comboBoxIndicator.Items.Add(allShares[i]);

                }
            }
            else
            {
                foreach (string str in IndEvaluator.IndicatorsAvailableNames)
                {
                    this.comboBoxIndicator.Items.Add(str);

                }

            }

          
        }
     
		private void PopulateControls()
		{
            if (this.indicatorElement.IsShare)
                this.checkBoxIsShare.Checked = true;
            else
                this.checkBoxIsShare.Checked = false;

            //called explicitly (even if called twice when checked changed ) 
            FillCombo_Indicators(this.indicatorElement.IsShare);

            foreach (string str in IndEvaluator.IndicatorsAvailableFunctionNames)
            {
                this.comboBoxIndicatorFunction.Items.Add(str);


            }

		}


		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		private void RaiseChangedEvent()
		{
			
			if( null != ConditionChanged )
			{

				ConditionChanged(this,new ConditionChangedEvent(this.IndexCondition, this.indicatorElement));
			}
			
		}
		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.textBoxIndicatorFunctionInterval = new System.Windows.Forms.TextBox();
            this.comboBoxIndicator = new System.Windows.Forms.ComboBox();
            this.textBoxBoxIndicatorInterval = new System.Windows.Forms.TextBox();
            this.comboBoxIndicatorFunction = new System.Windows.Forms.ComboBox();
            this.comboBoxAddOrSubstract = new System.Windows.Forms.ComboBox();
            this.textBoxFactor = new System.Windows.Forms.TextBox();
            this.labelElement = new System.Windows.Forms.Label();
            this.comboBoxFactor = new System.Windows.Forms.ComboBox();
            this.checkBoxIsShare = new System.Windows.Forms.CheckBox();
            this.SuspendLayout();
            // 
            // textBoxIndicatorFunctionInterval
            // 
            this.textBoxIndicatorFunctionInterval.Location = new System.Drawing.Point(497, 3);
            this.textBoxIndicatorFunctionInterval.Name = "textBoxIndicatorFunctionInterval";
            this.textBoxIndicatorFunctionInterval.Size = new System.Drawing.Size(56, 20);
            this.textBoxIndicatorFunctionInterval.TabIndex = 5;
            // 
            // comboBoxIndicator
            // 
            this.comboBoxIndicator.Location = new System.Drawing.Point(589, 0);
            this.comboBoxIndicator.Name = "comboBoxIndicator";
            this.comboBoxIndicator.Size = new System.Drawing.Size(216, 21);
            this.comboBoxIndicator.TabIndex = 6;
            // 
            // textBoxBoxIndicatorInterval
            // 
            this.textBoxBoxIndicatorInterval.Location = new System.Drawing.Point(881, 0);
            this.textBoxBoxIndicatorInterval.Name = "textBoxBoxIndicatorInterval";
            this.textBoxBoxIndicatorInterval.Size = new System.Drawing.Size(56, 20);
            this.textBoxBoxIndicatorInterval.TabIndex = 7;
            // 
            // comboBoxIndicatorFunction
            // 
            this.comboBoxIndicatorFunction.Location = new System.Drawing.Point(219, 3);
            this.comboBoxIndicatorFunction.Name = "comboBoxIndicatorFunction";
            this.comboBoxIndicatorFunction.Size = new System.Drawing.Size(256, 21);
            this.comboBoxIndicatorFunction.TabIndex = 4;
            // 
            // comboBoxAddOrSubstract
            // 
            this.comboBoxAddOrSubstract.Items.AddRange(new object[] {
            "+",
            "-",
            "*",
            "/"});
            this.comboBoxAddOrSubstract.Location = new System.Drawing.Point(53, 2);
            this.comboBoxAddOrSubstract.Name = "comboBoxAddOrSubstract";
            this.comboBoxAddOrSubstract.Size = new System.Drawing.Size(35, 21);
            this.comboBoxAddOrSubstract.TabIndex = 3;
            // 
            // textBoxFactor
            // 
            this.textBoxFactor.Location = new System.Drawing.Point(109, 4);
            this.textBoxFactor.Name = "textBoxFactor";
            this.textBoxFactor.Size = new System.Drawing.Size(40, 20);
            this.textBoxFactor.TabIndex = 12;
            this.textBoxFactor.Text = "1";
            // 
            // labelElement
            // 
            this.labelElement.Location = new System.Drawing.Point(3, 0);
            this.labelElement.Name = "labelElement";
            this.labelElement.Size = new System.Drawing.Size(31, 24);
            this.labelElement.TabIndex = 15;
            // 
            // comboBoxFactor
            // 
            this.comboBoxFactor.Items.AddRange(new object[] {
            "+",
            "-",
            "*",
            "/"});
            this.comboBoxFactor.Location = new System.Drawing.Point(165, 3);
            this.comboBoxFactor.Name = "comboBoxFactor";
            this.comboBoxFactor.Size = new System.Drawing.Size(48, 21);
            this.comboBoxFactor.TabIndex = 16;
            // 
            // checkBoxIsShare
            // 
            this.checkBoxIsShare.AutoSize = true;
            this.checkBoxIsShare.Location = new System.Drawing.Point(837, 6);
            this.checkBoxIsShare.Name = "checkBoxIsShare";
            this.checkBoxIsShare.Size = new System.Drawing.Size(15, 14);
            this.checkBoxIsShare.TabIndex = 17;
            this.checkBoxIsShare.UseVisualStyleBackColor = true;
            this.checkBoxIsShare.CheckedChanged += new System.EventHandler(this.checkBoxIsShare_CheckedChanged);
            // 
            // ConditionIndicatorBuilder
            // 
            this.Controls.Add(this.checkBoxIsShare);
            this.Controls.Add(this.comboBoxFactor);
            this.Controls.Add(this.labelElement);
            this.Controls.Add(this.textBoxFactor);
            this.Controls.Add(this.comboBoxAddOrSubstract);
            this.Controls.Add(this.comboBoxIndicatorFunction);
            this.Controls.Add(this.textBoxBoxIndicatorInterval);
            this.Controls.Add(this.comboBoxIndicator);
            this.Controls.Add(this.textBoxIndicatorFunctionInterval);
            this.Name = "ConditionIndicatorBuilder";
            this.Size = new System.Drawing.Size(1024, 24);
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

	
		public IndicatorElement IndicatorElement
		{
			get
			{
                BuildIndicatorFromControls();
				return this.indicatorElement;
			}
			set
			{
				this.indicatorElement = value;
			}
		}

		private void SetControlSelections()
		{
			string str = indicatorElement.OperatorString;
            //this.comboBoxAddOrSubstract.SelectedItem = str;
            this.comboBoxFactor.SelectedItem = str;

			textBoxFactor.Text =  Convert.ToString(indicatorElement.Factor);
			//this.comboBoxFactor.SelectedItem = indicatorElement.OperatorFactorString;
            this.comboBoxAddOrSubstract.SelectedItem = indicatorElement.OperatorFactorString;

            
			comboBoxIndicatorFunction.SelectedItem = indicatorElement.IndicatorFunction;
			textBoxIndicatorFunctionInterval.Text = indicatorElement.IndicatorFunctionInterval.ToString();

            comboBoxIndicator.SelectedItem = indicatorElement.IndicatorOrShare;
			textBoxBoxIndicatorInterval.Text = indicatorElement.IndicatorInterval.ToString();
			
		}

        private bool BuildIndicatorFromControls()
        {
            indicatorElement = new IndicatorElement();
          

            //.....................
            try
            {
                indicatorElement.OperatorFactorString = comboBoxAddOrSubstract.Text;
            }
            catch
            {

                indicatorElement.OperatorFactorString = "Error";
            }
            
            double f = 1f;
            try
            {
                f = Convert.ToDouble(textBoxFactor.Text);
            }
            catch
            {
                indicatorElement.Factor = f;
            }
            
            //---------------------------------------------------------
            try
            {
                indicatorElement.OperatorString = this.comboBoxFactor.Text;
            }
            catch
            {

                indicatorElement.OperatorFactorString = "Error";
            }
            //---------------------------------------------------------
            try
            {
                indicatorElement.IndicatorFunction = comboBoxIndicatorFunction.Text;

            }
            catch
            {

                indicatorElement.IndicatorFunction = "Error";

            }
            try
            {
                 string strREad = textBoxIndicatorFunctionInterval.Text;
                 int i = ReadValueAsInteger(strREad);
                 indicatorElement.IndicatorFunctionInterval = i;

            }
            catch
            {
                indicatorElement.IndicatorFunction = "-2";
             }


           
            //---------------------------------------------------------
            try
            {
                indicatorElement.IndicatorOrShare = this.comboBoxIndicator.Text;
               
            }
            catch
            {
                indicatorElement.IndicatorOrShare = "Error";
            }

            if (checkBoxIsShare.Checked)
            {
                indicatorElement.IsShare = true;
            }
            else
            {
                indicatorElement.IsShare = false;
            }

            //------------
            try
            {
                int i = indicatorElement.IndicatorInterval;
                string strREad = this.textBoxBoxIndicatorInterval.Text;
                i = ReadValueAsInteger(strREad);
                indicatorElement.IndicatorInterval = i;

            }
            catch
            {
                indicatorElement.IndicatorInterval = -2;
            }
            //---------------------------------------------------------
           

            return true;

        }
	
        private int ReadValueAsInteger(string strREad)
        {
            int i = 0;
            if (!string.IsNullOrEmpty(strREad))
            {
                try
                {
                    i = Convert.ToInt32(strREad);


                }
                catch
                {
                    i = 0;
                }
            }
            else
            {
                i = 0;
            }
            return i;
        }
        private void checkBoxIsShare_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxIsShare.Checked)
            {
                indicatorElement.IsShare = true;
            }
            else
            {
                indicatorElement.IsShare = false;
            }
            //called implicitely ???
            FillCombo_Indicators(this.checkBoxIsShare.Checked);

        }

        private void SetTypeToGlobal(object sender, EventArgs e)
        {
            //if (checkBoxShareType_Global.Checked)
            //{
            //    indicatorElement.IsGlobal = true;
            //}
            //else
            //{
            //    indicatorElement.IsGlobal = false;
            //}
            //PopulateComboBoxIndicator_Share();
        }

    
		
	}
}